# -*- coding: utf-8 -*-
"""
@Time: 2022/4/24 16:27
@Author: jins
@File: table_ods_device_electric_ts224.py
@Introduction: 
"""
import time
from datetime import datetime, timedelta
from threading import Thread

from common.mysqlDataOperate import OperateMysql


def insert(device_id_list):
    """
    多线程使用executemany方法插入mysql数据
    @return: None
    """
    om = OperateMysql(version=221)
    data_time = datetime(2022, 4, 1, 0, 0)
    data_time_list = []
    for _ in range(96 * 3):
        data_time_list.append(data_time)
        data_time += timedelta(minutes=15)
    # device_id_list = [277361]
    # 两个变量device_id和data_time组成笛卡儿积
    # res_list = [(device_id, str(data_time), str(data_time.timestamp())) for device_id in device_id_list for data_time in
    #             data_time_list]
    res_list = [(device_id, str(data_time), str(data_time)) for device_id in device_id_list for data_time in
                data_time_list]
    # print(res_list)
    sql_temp = "INSERT INTO data_center.ods_device_electric_ts224 VALUES(%s,%s,1.234,1.234,1.234,1.234,1.234,1.234,1.234," \
               "1.234,1.234,1.234,1.234,1.234,1.234,1.234,1.234,1.234,1.234,1.234,1.234,1.234,1.234,1.234,1.234," \
               "1.234,1.234,1.234,1.234,1.234,1.234,1.234,1.234,1.234,1.234,1.234,1.234,1.234,1.234,1.234,1.234," \
               "1.234,1.234,1.234,1.234,1.234,1.234,1.234,1.234,1.234,1.234,1.234,1.234,1.234,1.234,1.234,1.234," \
               "1.234,1.234,1.234,1,1,%s,'mi15')"
    om.excute_many(sql_temp, res_list)
    om.commit()
    om.close()


if __name__ == '__main__':
    device_ids_all = [277363, 280238, 605758, 605408, 605412, 605414, 605752, 605377, 605367, 605376, 605378, 605379,
                      605374, 665046, 665044, 665045, 665042, 665011, 665016, 665014, 658596, 658597, 658590, 658575,
                      658576, 658569, 658595, 658594, 672699, 672707, 580325, 580306, 580337, 595145, 633962, 595322,
                      633913, 595247, 595306, 595185, 595233, 595223, 595298, 595270, 595284, 672880, 580234, 580214,
                      672874, 659007, 659010, 659031, 659014, 659017, 659019, 659022, 659004, 659006, 280269, 280240,
                      281032, 280244, 280246, 280247, 280248, 280249, 280721, 280722, 280723, 532268, 532292, 530273,
                      532123, 532124, 532125, 532241, 532242, 532243, 532245, 532246, 562753, 562759, 562763, 532260,
                      532256, 562711, 562714, 532231, 532232, 532233, 532235, 532234, 532252, 532253, 561237, 532257,
                      532259, 532278, 532276, 532279, 532288, 532277, 532267, 532264, 532269, 284528, 284529, 284531,
                      271942, 271944, 271875, 271865, 271866, 271867, 271868, 271993, 271861, 271936, 279985, 279486,
                      279515, 279507, 279500, 279451, 279491, 279496, 279436, 279459, 279440, 279483, 279513, 279498,
                      279456, 279519, 279442, 279444, 279509, 291172, 279424, 279429, 280271, 279984, 280016, 280023,
                      280006, 280272, 279469, 280379, 278505, 278506, 278606, 278473, 273758, 278466, 278464, 278605,
                      272903, 272906, 280274, 280275, 280265, 280254, 272920, 272919, 280718, 272895, 272897, 272898,
                      276954, 276956, 276959, 276960, 277847, 277845, 276940, 277796, 276938, 272079, 272080, 277457,
                      272085, 272098, 272097, 277386, 272111, 272112, 272113, 272114, 272115, 272117, 272109, 272070,
                      272072, 272075, 271957, 271970, 271988, 271953, 271950, 271951, 272082, 272083, 272086, 272088,
                      281155, 272077, 272078, 280233, 280235, 280236, 280263, 280264, 280282, 280854, 280251, 280253,
                      280277, 280278, 280279, 284583, 279127, 271601, 271610, 279099, 271761, 271605, 271612, 271608,
                      279110, 279115, 279118, 279120, 273031, 272990, 273033, 283389, 281937, 283821, 278127, 278045,
                      278121, 278122, 278123, 278102, 278046, 278041, 278043, 278054, 278060, 282654, 278035, 273813,
                      278083, 278089, 278119, 284470, 282068, 281825, 284293, 284296, 284295, 282292, 282295, 282187,
                      282192, 281176, 282189, 282190, 282191, 282186, 558663, 558678, 558685, 558428, 558598, 558772,
                      558641, 626293, 558344, 558326, 558371, 558374, 558381, 558387, 558357, 626372, 558364, 558365,
                      558448, 626298, 626368, 558775, 569835, 558706, 558714, 558726, 558731, 532291, 532101, 572471,
                      557106, 557109, 557006, 557011, 532119, 572166, 532117, 532115, 532116, 302541, 302530, 302532,
                      343656, 343704, 352647, 394145, 343671, 394135, 343690, 394119, 352648, 343642, 343681, 352649,
                      343643, 343641, 343655, 343644, 353304, 353306, 353307, 353310, 353359, 353311, 353353, 353319,
                      353327, 353354, 353320, 352737, 352717, 352763, 352754, 352740, 352744, 352745, 352746, 354490,
                      352756, 352766, 352747, 352742, 341381, 341372, 341373, 341374, 341387, 341368, 384148, 384154,
                      341427, 342127, 342129, 342138, 342139, 342140, 342145, 342141, 342132, 342133, 342143, 366309,
                      342134, 342135, 366320, 366321, 341402, 341403, 341405, 344805, 344806, 360117, 343232, 343262,
                      343265, 343241, 343270, 343268, 351950, 343221, 343219, 343257, 343217, 343222, 343242, 360116,
                      341281, 344784, 353007, 344788, 341276, 344801, 341285, 353008, 344850, 341269, 344838, 344865,
                      344847, 341284, 341283, 344848, 344851, 344852, 344844, 352936, 341255, 341264, 341292, 352935,
                      352854, 341267, 341280, 341287, 344807, 341265, 341275, 341251, 341270, 344808, 344855, 344783,
                      341291, 353316, 692996, 343588, 343605, 360824, 343585, 343600, 343602, 343603, 343604, 283394,
                      283396, 283397, 283398, 281823, 281930, 281934, 279434, 279427, 279433, 282284, 282294, 282278,
                      282282, 282280, 282288, 279502, 353369, 353370, 341293, 342333, 341296, 341294, 343438, 343130,
                      343144, 343084, 343087, 343088, 343122, 343091, 343161, 343086, 343110, 343089, 343083, 343125,
                      343117, 343109, 343115, 343113, 343108, 343112, 343158, 343114, 343085, 353378, 353380, 353385,
                      353387, 352713, 352835, 352844, 352828, 352840, 352841, 352843, 352961, 352730, 352733, 352751,
                      352846, 352847, 352834, 352962, 343442, 343446, 343447, 343450, 343448, 343454, 343455, 343451,
                      343456, 343452, 343439, 343441, 341384, 341385, 341388, 341386, 341383, 341389, 341254, 341256,
                      341258, 341250, 341266, 341257, 341261, 341268, 341253, 352857, 352862, 352931, 352809, 352891,
                      352901, 353010, 353011, 352925, 352927, 341656, 341653, 341669, 341670, 341664, 341660, 341658,
                      341662, 341654, 341655, 302602, 302550, 302551, 302549, 302557, 302547, 318571, 318564, 318577,
                      353392, 341246, 341245, 341238, 574458, 574454, 574461, 357630, 412673, 412699, 353017, 353020,
                      353022, 358395, 353013, 341415, 341416, 341417, 342118, 342119, 342120, 342121, 342122, 342137,
                      342124, 341421, 341410, 341413, 343544, 343545, 343547, 733442, 738766, 343571, 343564, 343563,
                      343562, 343549, 343537, 343543, 341196, 341172, 341199, 341163, 341198, 341174, 341197, 341144,
                      341145, 341142, 341143, 341177, 341176, 341194, 341195, 318590, 318595, 318596, 343597, 343591,
                      341686, 341663, 341659, 341680, 341689, 341673, 705906, 705907, 345938, 343246, 343247, 343250,
                      343252, 345944, 342937, 342933, 342940, 342941, 342942, 342943, 342944, 318648, 318649, 318643,
                      318646, 318645, 318644, 318651, 318650, 352938, 352940, 353477, 352939, 341452, 341459, 341460,
                      341455, 341463, 341464, 341473, 341474, 341466, 341467, 341469, 341470, 341478, 341476, 341451,
                      341454, 341453, 341456, 341477, 413297, 413296, 708387, 708388, 708389, 708391, 559212, 559213,
                      559205, 559210, 559408, 559420, 559593, 559316, 559312, 559323, 559444, 559457, 559432, 564872,
                      559211, 564870, 559365, 413091, 413094, 413078, 413080, 413082, 413084, 510711, 413089, 413464,
                      413469, 418817, 418818, 413452, 413461, 413455, 413466, 413463, 413468, 417807, 417809, 417808,
                      430037, 429806, 429810, 430011, 655678, 655679, 655684, 655677, 655688, 655680, 655686, 655687,
                      655689, 566317, 566292, 566314, 658580, 658582, 658583, 658584, 658608, 658610, 658571, 658572,
                      658573, 569570, 569492, 569542, 564949, 564935, 564988, 682451, 682452, 656031, 656033, 655967,
                      655990, 655993, 656006, 656965, 656955, 656956, 656967, 656960, 656958, 656959, 656963, 656277,
                      656270, 656273, 656177, 656178, 656186, 656146, 656135, 656130, 656127, 656175, 656176, 593905,
                      593930, 593902, 593910, 593864, 593876, 593923, 593932, 654099, 654085, 657000, 657001, 653992,
                      580722, 580776, 656709, 565197, 565147, 565148, 565137, 565143, 656221, 656215, 656216, 656218,
                      656225, 656223, 656224, 656220, 583969, 558316, 526560, 583973, 584051, 558794, 653741, 653748,
                      653745, 653669, 592699, 592686, 592694, 656573, 654938, 654937, 654950, 656611, 656576, 656578,
                      656581, 654164, 655460, 687492, 655476, 655464, 655465, 655466, 687495, 655459, 563795, 563825,
                      563798, 563801, 563802, 563831, 653880, 653802, 653815, 653843, 653853, 412449, 412448, 414117,
                      414129, 414138, 414132, 414137, 414128, 417811, 417813, 417814, 417816, 417815, 417812, 412443,
                      412444, 412435, 412437, 412442, 708318, 708320, 708319, 708322, 708323, 414012, 414010, 624046,
                      414009, 416180, 416181, 416186, 416187, 416179, 414122, 412441, 412438, 623751, 623753, 414131,
                      414130, 412703, 412702, 412700, 412755, 412743, 412749, 412747, 412746, 412756, 412750, 418150,
                      418152, 418153, 418127, 418139, 553611, 412330, 412404, 413314, 413315, 413322, 413286, 413287,
                      413288, 415818, 415802, 415811, 415806, 415795, 415797, 415798, 518255, 415792, 625026, 625029,
                      625030, 625031, 625032, 412386, 412661, 412704, 412676, 412705, 412678, 413064, 413053, 413054,
                      414019, 414031, 414018, 414029, 414030, 623793, 623794, 623795, 623796, 417823, 417824, 414139,
                      414140, 414569, 414566, 412453, 412445, 412446, 417716, 417723, 417717, 417729, 415778, 415779,
                      415777, 415888, 415886, 415877, 415891, 415889, 415878, 415882, 415883, 415884, 415879, 414016,
                      524240, 523954, 523955, 523958, 530447, 530454, 530540, 681778, 605430, 605842, 605845, 605432,
                      605816, 605808, 605806, 658600, 658602, 658605, 658606, 658598, 623744, 413924, 413925, 411595,
                      411603, 411604, 411606, 416592]
    tmp = [device_ids_all[i:i + 4:] for i in range(250)]
    start_time = time.time()
    thread_list = []
    for i in tmp:
        t = Thread(target=insert, args=(i,))
        t.start()
        thread_list.append(t)
    for i in thread_list:
        i.join()
    end_time = time.time()
    res = end_time - start_time
    print(res)
